﻿package com.tudou.ad.components
{
    import com.tudou.ad.*;
    import com.tudou.ad.components.events.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;

    public class TudouAdSprite extends Sprite
    {
        public var allowClickAll:Boolean = false;
        protected var background:DisplayObject;
        protected var orginalWidth:uint;
        protected var orginalHeight:uint;
        protected var adDisplay:DisplayObject;

        public function TudouAdSprite(param1:Number, param2:Number, param3:uint = 0, param4:Number = 0)
        {
            orginalWidth = param1;
            orginalHeight = param2;
            background = RectDisplayUtil.getShape(param1, param2, param3, param4);
            addChild(background);
            var _loc_5:* = new Shape();
            RectDisplayUtil.drawRect(_loc_5.graphics, param1, param2, 3355443, 1);
            addChild(_loc_5);
            this.mask = _loc_5;
            return;
        }// end function

        protected function removeAdEvent(param1:IEventDispatcher) : void
        {
            param1.removeEventListener(MouseEvent.CLICK, clickedHandler);
            param1.removeEventListener(TudouAdEvent.AD_CLICK, clickedHandler);
            return;
        }// end function

        protected function clickedHandler(event:Event) : void
        {
            var _loc_4:String = null;
            var _loc_2:* = event as MouseEvent;
            var _loc_3:* = event as TudouAdEvent;
            if (adDisplay is InteractiveObject)
            {
                if (!allowClickAll)
                {
                    if (_loc_2 != null && _loc_2.type == MouseEvent.CLICK && isNaN(_loc_2.localX))
                    {
                        _loc_4 = "";
                        this.dispatchEvent(new TudouAdSpriteEvent(TudouAdSpriteEvent.CLICK, _loc_4));
                    }
                    else if (_loc_3 != null && _loc_3.type == TudouAdEvent.AD_CLICK)
                    {
                        _loc_4 = _loc_3.data == null ? ("") : (_loc_3.data.toString());
                        this.dispatchEvent(new TudouAdSpriteEvent(TudouAdSpriteEvent.CLICK, _loc_4));
                    }
                }
                else
                {
                    _loc_4 = "";
                    this.dispatchEvent(new TudouAdSpriteEvent(TudouAdSpriteEvent.CLICK, _loc_4));
                }
            }
            else
            {
                _loc_4 = "";
                this.dispatchEvent(new TudouAdSpriteEvent(TudouAdSpriteEvent.CLICK, _loc_4));
            }
            return;
        }// end function

        public function addAdDisplay(param1:DisplayObject) : void
        {
            this.useHandCursor = true;
            this.buttonMode = true;
            adDisplay = param1;
            this.addChild(adDisplay);
            if (adDisplay is InteractiveObject)
            {
                addAdEvent(adDisplay);
            }
            else
            {
                addAdEvent(this);
                adDisplay.width = orginalWidth;
                adDisplay.height = orginalHeight;
            }
            return;
        }// end function

        override public function get width() : Number
        {
            return background.width;
        }// end function

        override public function get height() : Number
        {
            return background.height;
        }// end function

        public function removeAdDisplay() : void
        {
            this.useHandCursor = false;
            this.buttonMode = false;
            if (adDisplay != null && this.contains(adDisplay))
            {
                this.removeChild(adDisplay);
                if (adDisplay is InteractiveObject)
                {
                    removeAdEvent(adDisplay);
                }
                else
                {
                    removeAdEvent(this);
                }
                adDisplay = null;
            }
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            setWidthHeight(param1, height);
            return;
        }// end function

        protected function completeHandler(event:Event) : void
        {
            this.dispatchEvent(new TudouAdSpriteEvent(TudouAdSpriteEvent.COMPLETE));
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            setWidthHeight(width, param1);
            return;
        }// end function

        public function setWidthHeight(param1:Number, param2:Number) : void
        {
            background.width = param1;
            background.height = param2;
            this.mask.width = param1;
            this.mask.height = param2;
            if (adDisplay is InteractiveObject)
            {
                RectDisplayUtil.putChildByScale(adDisplay, orginalWidth, orginalHeight, param1, param2);
            }
            else
            {
                RectDisplayUtil.putChild(adDisplay, param1, param2);
            }
            return;
        }// end function

        protected function addAdEvent(param1:IEventDispatcher) : void
        {
            param1.addEventListener(MouseEvent.CLICK, clickedHandler);
            param1.addEventListener(TudouAdEvent.AD_CLICK, clickedHandler);
            param1.addEventListener(Event.COMPLETE, completeHandler);
            param1.addEventListener(TudouAdEvent.AD_COMPLETE, completeHandler);
            return;
        }// end function

    }
}
